package constant.milk.toilet.stats;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import constant.milk.toilet.R;
import constant.milk.toilet.SuperActivity;
import constant.milk.toilet.bean.StatsData;
import constant.milk.toilet.bean.TimerData;
import constant.milk.toilet.database.DbSelectQuery;
import constant.milk.toilet.dialog.MilkStatsDialog;
import constant.milk.toilet.util.MilkUtil;

public class StatsActivity extends SuperActivity
{
	private TextView preView;
	private TextView nextView;
	private TextView dateTextView;
	
	private TextView countTextView;
	private TextView timeTextView;
	
	private ViewGroup listView;
	
	private int year;
	private int month;
	private int day;
	
	private int year1;
	private int month1;
	private int day1;
	
	private int year2;
	private int month2;
	private int day2;
	
	private MilkStatsDialog statsDialog;
	
	private DbSelectQuery select;
	
	private int weekday;
	
	private int flag;
	
	private boolean countFlag;
	private boolean timeFlag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		setAdFlag(false);
		
		super.onCreate(savedInstanceState);
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.stats_activty);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		preView = (TextView) findViewById(R.id.statsDatePreTextView);
		nextView = (TextView) findViewById(R.id.statsDateNextTextView);
		dateTextView = (TextView) findViewById(R.id.statsDateTextView);
		
		countTextView = (TextView) findViewById(R.id.statsCountTextView);
		timeTextView = (TextView) findViewById(R.id.statsTimeTextView);
		
		listView = (ViewGroup) findViewById(R.id.statsListView);
	}

	@Override
	protected void setOnClick()
	{
		preView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(year, month, day);
				if (flag == 0)
				{
					cal.add(Calendar.YEAR, -1);
				}
				else if (flag == 1)
				{
					cal.add(Calendar.MONTH, -1);
				}
				else if (flag == 2)
				{
					cal.add(Calendar.DAY_OF_MONTH, -7);
				}
				
				year = cal.get(Calendar.YEAR);
				month = cal.get(Calendar.MONTH);
				day = cal.get(Calendar.DAY_OF_MONTH);
				
				setDate();
			}
		});
		
		nextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(year, month, day);
				if (flag == 0)
				{
					cal.add(Calendar.YEAR, 1);
				}
				else if (flag == 1)
				{
					cal.add(Calendar.MONTH,1);
				}
				else if (flag == 2)
				{
					cal.add(Calendar.DAY_OF_MONTH, 7);
				}
				
				year = cal.get(Calendar.YEAR);
				month = cal.get(Calendar.MONTH);
				day = cal.get(Calendar.DAY_OF_MONTH);
				
				setDate();
			}
		});

		dateTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				statsDialog = new MilkStatsDialog(superContext, 
						new MilkStatsDialog.StatsCollBank()
				{
					
					@Override
					public void getStats(int stats)
					{
						flag = stats;
						setDate();
						statsDialog.dismiss();
					}
				});
				
				statsDialog.show();
			}
		});
		
		countTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (countFlag)
				{
					countFlag = false;
					countTextView.setBackgroundResource(R.drawable.stats_count_def);
				}
				else
				{
					countFlag = true;
					countTextView.setBackgroundResource(R.drawable.stats_count_push);
				}
				
				setDate();
			}
		});
		
		timeTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (timeFlag)
				{
					timeFlag = false;
					timeTextView.setBackgroundResource(R.drawable.stats_time_def);
				}
				else
				{
					timeFlag = true;
					timeTextView.setBackgroundResource(R.drawable.stats_time_push);
				}
				
				setDate();
			}
		});
	}

	@Override
	protected void setData()
	{
		select = new DbSelectQuery(superContext, superDb);
		
		Calendar cal = Calendar.getInstance();
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		flag = 1;
		countFlag = true;
		timeFlag = true;
		
		setDate();
	}
	
	private void setDate()
	{
		if (flag == 0)
		{
			setYear();
		}
		else if (flag == 1)
		{
			setMonth();
		}
		else if (flag == 2)
		{
			setWeekDay();
		}
		
		setDateText();
		setStatsData();
	}
	
	private void setStatsData()
	{
		ArrayList<TimerData> countList = new ArrayList<TimerData>();
		ArrayList<TimerData> termList = new ArrayList<TimerData>();
		ArrayList<StatsData> statsList = new ArrayList<StatsData>();
		
		if (flag == 0)
		{
			countList = select.getTimerYearCount(year);
			termList = select.getTimerYearTerm(year);
			
			for (int i = 0; i < 12; i++)
			{
				StatsData statsData = new StatsData();
				statsData.setTitle((i+1) + "월");
				
				statsList.add(statsData);
			}
			
		}
		else if (flag == 1)
		{
			Calendar cal = Calendar.getInstance();
			cal.set(year, month, day);
			
			countList = select.getTimerMonthCount(year, month);
			termList = select.getTimerMonthTerm(year, month);
			
			for (int i = 0; i < cal.getActualMaximum(Calendar.DAY_OF_MONTH); i++)
			{
				StatsData statsData = new StatsData();
				statsData.setTitle((i+1) + "일");
				
				statsList.add(statsData);
			}
		}
		else if (flag == 2)
		{
			countList = select.getTimerWeekdayCount(year1, month1, day1, year2, month2, day2);
			termList = select.getTimerWeekdayTerm(year1, month1, day1, year2, month2, day2);
			
			for (int i = 0; i < 7; i++)
			{
				StatsData statsData = new StatsData();
				statsData.setTitle(MilkUtil.getWeekDay(i+1));
				
				statsList.add(statsData);
			}
		}
		
		BigDecimal maxC = getMaxCountData(countList);
		BigDecimal maxT = getMaxTermData(termList);
		
		for (int i = 0; i < statsList.size(); i++)
		{
			StatsData statsData = statsList.get(i);
			
			for (int j = 0; j < countList.size(); j++)
			{
				TimerData data = countList.get(j);
				
				int y = 0;
				if (flag == 0)
				{
					y = data.getMonth();
				}
				else if (flag == 1)
				{
					y = data.getDay()-1;
				}
				else
				{
					y = data.getDay();
				}
				
				if (y == i)
				{
					BigDecimal count = new BigDecimal(countList.get(j).getCount());
					
					count = count.divide(maxC, 2, BigDecimal.ROUND_UP);
					
					count = count.multiply(new BigDecimal(100));
					
					statsData.setCountPercent(count.toString());
					statsData.setCount(countList.get(j).getCount());
				}
			}
			
			for (int j = 0; j < termList.size(); j++)
			{
				TimerData data = termList.get(j);
				
				int y = 0;
				if (flag == 0)
				{
					y = data.getMonth();
				}
				else if (flag == 1)
				{
					y = data.getDay()-1;
				}
				else
				{
					y = data.getDay();
				}
				
				if (y == i)
				{
					BigDecimal term = new BigDecimal(termList.get(j).getTerm());
					
					term = term.divide(maxT, 2, BigDecimal.ROUND_UP);
					
					term = term.multiply(new BigDecimal(100));
					
					statsData.setTimePercent(term.toString());
					statsData.setTime(getTermTime(Long.parseLong(termList.get(j).getTerm())));
				}
			}
		}
		
		setListData(statsList);
	}

	private void setDateText()
	{
		if (flag == 0)
		{
			dateTextView.setText(year + "");
		}
		else if (flag == 1)
		{
			dateTextView.setText(year + ". " + (month+1));
		}
		else if (flag == 2)
		{
			Calendar cal = Calendar.getInstance();
			cal.set(year, month, day);
			int weekDay = cal.get(Calendar.WEEK_OF_MONTH);
			
			dateTextView.setText(year + ". " + (month+1) + ". " + weekDay + "째주");
		}
	}
	
	private void setYear()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
	}
	
	private void setMonth()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
	}
	
	private void setWeekDay()
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.set(year, month, day);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(year, month, day);
		
		weekday = cal2.get(Calendar.DAY_OF_WEEK);
		
		cal1.add(Calendar.DAY_OF_MONTH, -(weekday - 1));
		cal2.add(Calendar.DAY_OF_MONTH, (7 - weekday));
		
		year1 = cal1.get(Calendar.YEAR);
		month1 = cal1.get(Calendar.MONTH);
		day1 = cal1.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
	}
	
	private void setListData(ArrayList<StatsData> list)
	{
		listView.removeAllViews();
		
		for (int i = 0; i < list.size(); i++)
		{
			StatsData data = list.get(i);
			
			setStats(data.getTitle(), 
					Float.parseFloat(data.getCountPercent()), 
					Float.parseFloat(data.getTimePercent()), 
					data.getCount(), 
					data.getTime());
		}
		
	}

	private void setStats(String titleStr, Float countWeight, Float timeWeight, String countStr, String timeStr)
	{
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.list_item_stats, null);
		
		LinearLayout.LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		
		TextView title = (TextView) v.findViewById(R.id.listItemStatsTitleTextView);
		View countView = v.findViewById(R.id.listItemStatsCountView);
		TextView count = (TextView) v.findViewById(R.id.listItemStatsCountTextView);
		TextView countTitle = (TextView) v.findViewById(R.id.listItemStatsCountTitleTextView);
		View timeView = v.findViewById(R.id.listItemStatsTimeView);
		TextView time = (TextView) v.findViewById(R.id.listItemStatsTimeTextView);
		TextView timeTitle = (TextView) v.findViewById(R.id.listItemStatsTimeTitleTextView);
		
		LinearLayout.LayoutParams params1 = (LayoutParams) count.getLayoutParams();
		params1.weight = countWeight;
		
		LinearLayout.LayoutParams params2 = (LayoutParams) time.getLayoutParams();
		params2.weight = timeWeight;
		
		title.setTextSize(16);
		title.setText(titleStr);
		
		if (countWeight != 0)
		{
			countTitle.setTextSize(16);
			countTitle.setText(countStr + "번");
		}
		
		if (timeWeight != 0)
		{
			timeTitle.setTextSize(16);
			timeTitle.setText(timeStr);
		}
		
		if (countFlag)
		{
			countView.setVisibility(View.VISIBLE);
		}
		else
		{
			countView.setVisibility(View.GONE);
		}
		
		if (timeFlag)
		{
			timeView.setVisibility(View.VISIBLE);
		}
		else
		{
			timeView.setVisibility(View.GONE);
		}
		
		v.setLayoutParams(p);
		
		listView.addView(v);
	}
	
	private String getTermTime(long termDate)
	{
		Calendar calT = Calendar.getInstance();
		calT.setTimeInMillis(termDate);
		
		String termStr = "";
		
		int year = calT.get(Calendar.YEAR);
		int month = calT.get(Calendar.MONTH);
		int day = calT.get(Calendar.DAY_OF_MONTH);
		int hour = calT.get(Calendar.HOUR_OF_DAY);
		int minute = calT.get(Calendar.MINUTE);
		int second = calT.get(Calendar.SECOND);
		
		year = year - 1970;
		month = month -1;
		day = day - 1;
		hour = hour - 9;
		
		if (year > 0)
		{
			termStr = termStr + year + "년 ";
		}
		
		if (month > -1)
		{
			termStr = termStr + (month+1) + "월 ";
		}
		
		if (day > 0)
		{
			termStr = termStr + day + "일 ";
		}
		
		if (hour > 0)
		{
			termStr = termStr + hour + "시간 ";
		}
		
		if (minute > 0)
		{
			termStr = termStr + minute + "분 ";
		}
		
		if (second > 0)
		{
			termStr = termStr + second + "초";
		}
		
		if (termStr.equals(""))
		{
			termStr = "0초";
		}
		
		return termStr;
	}
	
	private BigDecimal getMaxCountData(ArrayList<TimerData> list)
	{
		BigDecimal max = BigDecimal.ZERO;
		
		for (int i = 0; i < list.size(); i++)
		{
			TimerData data = list.get(i);
			
			if (max.compareTo(new BigDecimal(data.getCount())) < 0)
			{
				max = new BigDecimal(data.getCount());
			}
		}
		
		return max;
	}
	
	private BigDecimal getMaxTermData(ArrayList<TimerData> list)
	{
		BigDecimal max = BigDecimal.ZERO;
		
		for (int i = 0; i < list.size(); i++)
		{
			TimerData data = list.get(i);
			
			if (max.compareTo(new BigDecimal(data.getTerm())) < 0)
			{
				max = new BigDecimal(data.getTerm());
			}
		}
		
		return max;
	}
}
